Skip to content

fix: tanstack query integration#418

Merged
tannerlinsley merged 4 commits intomainfrom
tanstack-query-fix
Mar 31, 2026
Merged

fix: tanstack query integration#418
tannerlinsley merged 4 commits intomainfrom
tanstack-query-fix

Conversation

@brenelz
Copy link
Copy Markdown
Contributor

@brenelz brenelz commented Mar 30, 2026

Tested locally with

node ./cli/packages/cli/dist/index.js create my-app3 --add-ons tanstack-query

Summary by CodeRabbit

  • Refactor

    • Query client is created earlier during router setup and passed into the app for SSR integration.
    • Server-side query integration is registered during router creation for improved SSR behavior.
    • TanStack Query provider simplified: when RPC is enabled it now accepts an explicit context prop; otherwise the provider surface is reduced.
  • Chores

    • Provider integration entry removed from add-on metadata, leaving only the devtools integration.
    • Added a patch changeset for the fix.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f295ff65-79b6-4f39-9f62-28b4942c9699

📥 Commits

Reviewing files that changed from the base of the PR and between 46ec412 and 4f7f7b4.

📒 Files selected for processing (1)
  • .changeset/soft-corners-allow.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/soft-corners-allow.md

📝 Walkthrough

Walkthrough

Removed module-level cached context: getContext() now returns a fresh context per call. Callers create and pass context to router creation, SSR integration, and (when tRPC-enabled) to TanstackQueryProvider. The provider no longer constructs or caches its own QueryClient.

Changes

Cohort / File(s) Summary
Provider context refactor
packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs
Removed module-scoped cached context and the early-return in getContext(). getContext() now constructs and returns a fresh context each call. TanstackQueryProvider signature changed: tRPC branch accepts a context prop and uses its queryClient; non-tRPC branch provider implementation was replaced with an empty component.
Integration metadata
packages/create/src/frameworks/react/add-ons/tanstack-query/info.json
Removed the provider integration entry from integrations, leaving only the devtools integration.
Router setup changes
packages/create/src/frameworks/react/project/base/src/router.tsx.ejs
Router creation now captures context = getContext() once, passes context into createTanStackRouter, uses context.queryClient with setupRouterSsrQueryIntegration, updates imports, and (when tRPC is enabled) sets router Wrap to render TanstackQueryProvider with the computed context.
Release metadata
.changeset/soft-corners-allow.md
Added a changeset to publish a patch for @tanstack/create titled "Fix Tanstack Query Integration".

Sequence Diagram(s)

sequenceDiagram
    participant RouterSetup as Router Setup
    participant getContext as getContext()
    participant RouterFactory as createTanStackRouter
    participant Provider as TanstackQueryProvider
    participant SSR as setupRouterSsrQueryIntegration
    participant QueryClient as QueryClient

    RouterSetup->>getContext: call
    getContext-->>RouterSetup: return context (includes QueryClient)
    RouterSetup->>RouterFactory: createTanStackRouter(..., context)
    RouterSetup->>SSR: setupRouterSsrQueryIntegration({ router, queryClient: context.queryClient })
    RouterSetup->>Provider: render Wrap with context prop (when tRPC)
    Provider->>QueryClient: read/use queryClient from context
    SSR-->>RouterSetup: SSR integration configured
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐇 I nibbled the cache and gave it a shove,
Fresh context for each call — a hop and a love.
Router and provider now pass hand in paw,
Queries lined up neat — no mysterious flaw.
🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: tanstack query integration' directly and concisely summarizes the main change - it fixes the TanStack Query integration by refactoring how the provider handles context and QueryClient instantiation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tanstack-query-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs (1)

40-45: ⚠️ Potential issue | 🔴 Critical

Return the new context object directly instead of assigning to an undeclared variable.

The context variable at line 40 is assigned without being declared. This will cause a runtime error in the generated react + tanstack-query + tRPC template.

Suggested fix
 export function getContext() {
   const queryClient = new QueryClient({
     defaultOptions: {
       dehydrate: { serializeData: superjson.serialize },
       hydrate: { deserializeData: superjson.deserialize },
     },
   });

   const serverHelpers = createTRPCOptionsProxy({
     client: trpcClient,
     queryClient: queryClient,
   });
-  context = {
+  return {
     queryClient,
     trpc: serverHelpers,
   }
-
-  return context
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`
around lines 40 - 45, Replace the undeclared assignment to context with a direct
return of the object: instead of setting context = { queryClient, trpc:
serverHelpers } and returning context, return { queryClient, trpc: serverHelpers
} directly from the function (locate the return in root-provider.tsx.ejs where
queryClient and serverHelpers are in scope).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`:
- Around line 40-45: Replace the undeclared assignment to context with a direct
return of the object: instead of setting context = { queryClient, trpc:
serverHelpers } and returning context, return { queryClient, trpc: serverHelpers
} directly from the function (locate the return in root-provider.tsx.ejs where
queryClient and serverHelpers are in scope).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8f586686-6d53-466e-be20-d9ce66d07b10

📥 Commits

Reviewing files that changed from the base of the PR and between 40b9c12 and ac8d561.

📒 Files selected for processing (3)
  • packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs
  • packages/create/src/frameworks/react/add-ons/tanstack-query/info.json
  • packages/create/src/frameworks/react/project/base/src/router.tsx.ejs
💤 Files with no reviewable changes (1)
  • packages/create/src/frameworks/react/add-ons/tanstack-query/info.json

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs (1)

28-45: ⚠️ Potential issue | 🔴 Critical

Return the fresh context directly.

Line 40 assigns to context without any declaration in this template, so the generated code will fail with a reference error at runtime or fail TypeScript compilation with Cannot find name 'context'. The TanStackQueryProvider component expects this function to return the context object.

🔧 Proposed fix
 export function getContext() {
   const queryClient = new QueryClient({
     defaultOptions: {
       dehydrate: { serializeData: superjson.serialize },
       hydrate: { deserializeData: superjson.deserialize },
     },
   });

   const serverHelpers = createTRPCOptionsProxy({
     client: trpcClient,
-    queryClient: queryClient,
+    queryClient,
   });
-  context = {
+  return {
     queryClient,
     trpc: serverHelpers,
   }
-
-  return context
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`
around lines 28 - 45, The getContext function currently assigns to an undeclared
variable "context" causing a reference/TS error; change getContext to return the
fresh context object directly (or declare a local const if you need the
variable), e.g. build a local const (e.g., const context = { queryClient, trpc:
serverHelpers }) and then return that const; update references to
getContext/TanStackQueryProvider to use the returned object and avoid using a
global "context" variable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`:
- Around line 28-45: The getContext function currently assigns to an undeclared
variable "context" causing a reference/TS error; change getContext to return the
fresh context object directly (or declare a local const if you need the
variable), e.g. build a local const (e.g., const context = { queryClient, trpc:
serverHelpers }) and then return that const; update references to
getContext/TanStackQueryProvider to use the returned object and avoid using a
global "context" variable.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 883dad3c-7b50-4797-a17a-c0b9a701c294

📥 Commits

Reviewing files that changed from the base of the PR and between ac8d561 and 8f9fcc3.

📒 Files selected for processing (2)
  • packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs
  • packages/create/src/frameworks/react/project/base/src/router.tsx.ejs
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/create/src/frameworks/react/project/base/src/router.tsx.ejs

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs (1)

3-3: ⚠️ Potential issue | 🔴 Critical

Missing QueryClientProvider wrapper: The return statement must wrap TRPCProvider with QueryClientProvider.

Per official tRPC documentation, QueryClientProvider must wrap TRPCProvider to provide the QueryClient context. Currently, QueryClientProvider is imported on line 3 but never used. The component at lines 57-61 renders only TRPCProvider without the required wrapper, which will cause React Query hooks to fail.

The fix is:

return (
  <QueryClientProvider client={queryClient}>
    <TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
      {children}
    </TRPCProvider>
  </QueryClientProvider>
)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`
at line 3, The component currently imports QueryClientProvider but does not use
it — wrap the existing TRPCProvider with QueryClientProvider so React Query
context is available: open the component that returns TRPCProvider (use symbols
QueryClientProvider, TRPCProvider, queryClient, trpcClient, children to locate
it) and change the return so QueryClientProvider receives client={queryClient}
and encloses TRPCProvider (which should still get trpcClient and queryClient)
with children rendered inside the inner provider.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`:
- Around line 63-73: The TanstackQueryProvider component currently renders
nothing and drops children; update TanstackQueryProvider to accept props ({
children, context? }) and render <QueryClientProvider
client={context?.queryClient ??
getContext().queryClient}>{children}</QueryClientProvider>, ensuring it uses the
QueryClient from getContext() when no context prop is provided and forwards
children so TanStack Query hooks work outside the tRPC branch; reference the
TanstackQueryProvider and getContext functions and the
QueryClient/QueryClientProvider symbols when making the change.

---

Outside diff comments:
In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`:
- Line 3: The component currently imports QueryClientProvider but does not use
it — wrap the existing TRPCProvider with QueryClientProvider so React Query
context is available: open the component that returns TRPCProvider (use symbols
QueryClientProvider, TRPCProvider, queryClient, trpcClient, children to locate
it) and change the return so QueryClientProvider receives client={queryClient}
and encloses TRPCProvider (which should still get trpcClient and queryClient)
with children rendered inside the inner provider.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0d932aa7-654f-4f83-8643-d2d447f4d85c

📥 Commits

Reviewing files that changed from the base of the PR and between 8f9fcc3 and 46ec412.

📒 Files selected for processing (2)
  • packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs
  • packages/create/src/frameworks/react/project/base/src/router.tsx.ejs
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/create/src/frameworks/react/project/base/src/router.tsx.ejs

Comment on lines 63 to +73
<% } else { %>
import type { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

let context:
| {
queryClient: QueryClient
}
| undefined
import { QueryClient } from '@tanstack/react-query'

export function getContext() {
if (context) {
return context
}

const queryClient = new QueryClient();

context = {
return {
queryClient,
}

return context
}

export default function TanStackQueryProvider({
children,
}: {
children: ReactNode
}) {
const { queryClient } = getContext()

return (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
)
}
export default function TanstackQueryProvider() {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Non-tRPC branch renders nothing and discards children.

The TanstackQueryProvider component in the non-tRPC branch is completely empty:

  • It doesn't accept children or context props
  • It doesn't render QueryClientProvider
  • Any children passed to it are silently discarded

This breaks the TanStack Query integration when used without tRPC. Users will not have access to QueryClient context, and all useQuery/useMutation hooks will fail.

🐛 Proposed fix to provide QueryClient context
+import type { ReactNode } from 'react'
-import { QueryClient } from '@tanstack/react-query'
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

 export function getContext() {
   const queryClient = new QueryClient();

   return {
     queryClient,
   }
 }
-export default function TanstackQueryProvider() {}
+export default function TanstackQueryProvider({
+  children,
+  context,
+}: {
+  children: ReactNode,
+  context: ReturnType<typeof getContext>
+}) {
+  return (
+    <QueryClientProvider client={context.queryClient}>
+      {children}
+    </QueryClientProvider>
+  )
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<% } else { %>
import type { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
let context:
| {
queryClient: QueryClient
}
| undefined
import { QueryClient } from '@tanstack/react-query'
export function getContext() {
if (context) {
return context
}
const queryClient = new QueryClient();
context = {
return {
queryClient,
}
return context
}
export default function TanStackQueryProvider({
children,
}: {
children: ReactNode
}) {
const { queryClient } = getContext()
return (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
)
}
export default function TanstackQueryProvider() {}
<% } else { %>
import type { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
export function getContext() {
const queryClient = new QueryClient();
return {
queryClient,
}
}
export default function TanstackQueryProvider({
children,
context,
}: {
children: ReactNode,
context: ReturnType<typeof getContext>
}) {
return (
<QueryClientProvider client={context.queryClient}>
{children}
</QueryClientProvider>
)
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`
around lines 63 - 73, The TanstackQueryProvider component currently renders
nothing and drops children; update TanstackQueryProvider to accept props ({
children, context? }) and render <QueryClientProvider
client={context?.queryClient ??
getContext().queryClient}>{children}</QueryClientProvider>, ensuring it uses the
QueryClient from getContext() when no context prop is provided and forwards
children so TanStack Query hooks work outside the tRPC branch; reference the
TanstackQueryProvider and getContext functions and the
QueryClient/QueryClientProvider symbols when making the change.

@tannerlinsley tannerlinsley merged commit a0b407c into main Mar 31, 2026
4 checks passed
@tannerlinsley tannerlinsley deleted the tanstack-query-fix branch March 31, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants